<template>
  <div class="lb-dy-manage">
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          opType="add"
          icon="el-icon-plus"
          @click="addDynamic(0)"
          >添加普通{{ $t('menu.Dynamic') }}</lb-button
        >
        <lb-button
          type="primary"
          opType="add"
          icon="el-icon-plus"
          @click="addDynamic(2)"
          >添加链接{{ $t('menu.Dynamic') }}</lb-button
        >
        <lb-button
          type="primary"
          opType="add"
          icon="el-icon-plus"
          @click="addDynamic(1)"
          >添加视频{{ $t('menu.Dynamic') }}</lb-button
        >
      </el-row>
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="标题" prop="keyword">
            <el-input
              v-model="searchForm.keyword"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column label="图标">
          <template slot-scope="scope">
            <lb-image fit="cover" :src="scope.row.cover[0]" />
          </template>
        </el-table-column>
        <el-table-column prop="cate_name" label="所属分类"> </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column prop="company_or_user_name" label="发布人">
        </el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <el-tag>{{
              scope.row.type === 1
                ? '视频'
                : scope.row.type === 2
                ? '链接'
                : '普通'
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否上架">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateDynamicInfo(scope.row.id, scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="jumpRoute(scope.row.id, scope.row.type)"
                >{{ $t('action.edit') }}</lb-button
              >
              <!-- <lb-button size='mini' :disabled="scope.row.type === 3" plain type='primary' @click="jumpRoute(scope.row.id,scope.row.type)">{{$t('action.edit')}}</lb-button> -->
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        :selected="batchArr.length"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
        <lb-button
          size="mini"
          type="danger"
          @click="delConfirmDialog(-1, batchArr)"
          >批量删除</lb-button
        >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      searchForm: {
        keyword: '',
        page: 1,
        limit: 10
      },
      total: 0,
      tableData: [],
      batchArr: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    getTableDataList (flag) {
      this.tableData = []
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { searchForm } = this
      this.$api.getDynamicList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    addDynamic (type) {
      let url = type === 0 ? '/dynamic/editOrdinary' : type === 2 ? '/dynamic/editLink' : '/dynamic/editVideo'
      this.$router.push(url)
    },
    updateDynamicInfo (id, status) {
      this.$api.updateDynamicInfo({ timeline_id: id, method: status }).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(this.$t(status === 2 ? 'tips.successDel' : 'tips.successOper'))
          if (status === 2) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === 2) return
          this.getTableDataList()
        }
      })
    },
    jumpRoute (id, type) {
      let url = type === 1 ? `/dynamic/editVideo?id=${id}` : type === 2 ? `/dynamic/editLink?id=${id}` : `/dynamic/editOrdinary?id=${id}`
      this.$router.push(url)
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateDynamicInfo(id, 2)
      }).catch(() => {

      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 查看全部
     */
    lookAll (index) {
      if (!this.msgList[index].open) { this.msgList[index].open = true }
    },
    /**
     * @name: 批量选中
     * @param {*} selectedArr
     */
    handleSelectionChange (selectedArr) {
      this.batchArr = selectedArr.map(item => {
        return item.id
      })
    },
    /**
     * @method 删除确认
     */
    delConfirmDialog (status, batchArr) {
      if (batchArr.length < 1) {
        this.$message.error('请选择要操作的数据')
        return
      }
      this.$confirm(this.$t('tips.confirmOperate'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.batchUpperAndDel(status, batchArr)
      })
    },

    /**
     * @method 批量上下架删除
     */
    batchUpperAndDel (status, selectedArr) {
      this.$api.dynamicBatchDel({ id: selectedArr }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },

  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px;
}
</style>
